﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using NUnit.Framework;
using AppToolkit.WPF.UI.Search;
using System.Windows.Input;
using UnitTestUtils.UI;
using System.ComponentModel;
using System.Windows.Data;

namespace AppToolkit.WPF.Tests.UI.Search
{
    [TestFixture, RequiresSTA]
    public class SearchContext_UI_Tests
    {
        private UIHost _host;
        SearchContext<string> _target;
        VM _vm;

        class VM : ViewModelBase
        {
            public VM(SearchContext<string> context)
            {
                this.SearchContext = context;
                this.View = CollectionViewSource.GetDefaultView(this.SearchContext.SourceCollection);
            }

            public SearchContext<string> SearchContext { get; private set; }

            public ICollectionView View { get; private set; }
        }


        [SetUp]
        public void Setup()
        {
            _target = new SearchContext<string>();
            initializeSearchContext();

            SearchWindow wnd = new SearchWindow();
            _vm = new VM(_target);
            wnd.DataContext = _vm;
            _host = UIHost.Create(wnd);
        }

        [TearDown]
        public void Dispose()
        {
            _target = null;
            UnitTestUtils.UI.UITestHelper.CleanupDispatcher();
        }

        /// <summary>
        /// Does provide search specification property.
        /// </summary>
        [Test,Explicit]
        public void Interactive_Can_set_SearchSpecification()
        {
            _host.ShowDialog();
        }
        
        #region Internals
        private void initializeSearchContext(int pos, string overrideValue)
        {
            Assert.That(pos, Is.InRange(0, 19));
            initializeSearchContext();

            string[] coll = (string[])_target.SourceCollection;
            coll[pos] = overrideValue;
        }

        private void initializeSearchContext()
        {
            string[] list = new string[]
            {
                "ABBA","Beatsteaks","Beatles","Chicago","Detroit","Emils", "Emilbulls", "Foals", "Good Fathers",
                "Helmet", "Jamiroqai", "Jungle Boyz", "The Killers", "The Kills",
            };

            _target.SourceCollection = list;
            _target.FilterSetGeneration = (sourceColl, filterSpec) =>
                {
                    return sourceColl.Where(p => p.Contains(filterSpec));
                };

            _target.SelectItem = i => 
            {
                _vm.View.MoveCurrentTo(i);
            };
        } 
        #endregion
    }
}
